package com.example.sxt_practise.IO;

import java.io.*;

/**
 * 对象流
 *      ObjectInputStream
 *      ObjectOutputStream
 *    只有实现了 Serializable 接口才能序列化
 * @author gaofeng
 * @date 2022年05月11日 23:21
 */
public class ObjectStreamTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ;
        ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(byteArrayOutputStream)) ;
        outputStream.writeUTF("编码辛酸泪,谁解其中味道");
        outputStream.writeInt(18);
        outputStream.writeBoolean(true);
        outputStream.writeObject(new User(18, "高枫"));
        outputStream.flush();

        byte[] data = byteArrayOutputStream.toByteArray();
        ObjectInputStream inputStream = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(data))) ;
        System.out.println(inputStream.readUTF());
        inputStream.readInt() ;
        inputStream.readBoolean() ;
        System.out.println(inputStream.readObject());

    }
}
